package com.selfdiscipline.ui.screens.task

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.selfdiscipline.R
import com.selfdiscipline.data.local.entity.TaskEntity
import com.selfdiscipline.ui.components.common.TopAppBarCommon
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TaskDetailScreen(
    taskId: Long,
    navigateBack: () -> Unit,
    viewModel: TaskDetailViewModel = hiltViewModel(),
    navigateToEditTask: (Long) -> Unit = {}
) {
    viewModel.loadTask(taskId)
    val uiState by viewModel.uiState.collectAsState()
    
    Scaffold(
        topBar = {
            TopAppBarCommon(
                title = stringResource(id = R.string.task_detail),
                onNavigationClick = navigateBack,
                actions = {
                    // 编辑按钮
                    IconButton(onClick = { navigateToEditTask(taskId) }) {
                        Icon(
                            imageVector = Icons.Filled.Edit,
                            contentDescription = "编辑任务"
                        )
                    }
                    // 删除按钮
                    IconButton(onClick = { 
                        viewModel.deleteTask(taskId)
                        navigateBack()
                    }) {
                        Icon(
                            imageVector = Icons.Filled.Delete,
                            contentDescription = "删除任务"
                        )
                    }
                }
            )
        }
    ) { paddingValues ->
        if (uiState.isLoading) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator()
            }
        } else if (uiState.error != null) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues),
                contentAlignment = Alignment.Center
            ) {
                Text(text = uiState.error ?: "发生错误")
            }
        } else {
            uiState.task?.let { task ->
                TaskDetailContent(
                    task = task,
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(paddingValues),
                    onCompleteTask = { viewModel.completeTask(taskId) }
                )
            }
        }
    }
}

@Composable
fun TaskDetailContent(
    task: TaskEntity,
    modifier: Modifier = Modifier,
    onCompleteTask: () -> Unit
) {
    Column(
        modifier = modifier.padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // 任务标题
        Text(
            text = task.title,
            style = MaterialTheme.typography.headlineMedium,
            fontWeight = FontWeight.Bold
        )
        
        // 任务描述
        if (!task.description.isNullOrEmpty()) {
            Text(
                text = task.description,
                style = MaterialTheme.typography.bodyLarge
            )
        }
        
        Divider()
        
        // 任务详情信息
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.fillMaxWidth()
        ) {
            Icon(
                imageVector = Icons.Filled.Flag,
                contentDescription = null,
                tint = when (task.priority) {
                    2 -> MaterialTheme.colorScheme.error
                    1 -> MaterialTheme.colorScheme.tertiary
                    else -> MaterialTheme.colorScheme.primary
                }
            )
            Spacer(modifier = Modifier.width(8.dp))
            Text(
                text = when (task.priority) {
                    2 -> "高优先级"
                    1 -> "中优先级"
                    else -> "低优先级"
                }
            )
        }
        
        // 截止日期
        task.dueDate?.let {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.fillMaxWidth()
            ) {
                Icon(
                    imageVector = Icons.Filled.DateRange,
                    contentDescription = null
                )
                Spacer(modifier = Modifier.width(8.dp))
                Text(text = "截止日期: ${it.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))}")
            }
        }
        
        Spacer(modifier = Modifier.weight(1f))
        
        // 完成按钮
        Button(
            onClick = onCompleteTask,
            modifier = Modifier.fillMaxWidth()
        ) {
            Icon(
                imageVector = if (task.isCompleted) Icons.Filled.Close else Icons.Filled.Check,
                contentDescription = null
            )
            Spacer(modifier = Modifier.width(8.dp))
            Text(text = if (task.isCompleted) "取消完成" else "标记为完成")
        }
    }
} 